/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * <p>Project: javaseapp - TimerDemo
 * <p>Powered by webrx On 2021-08-24 17:28:08
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class TimerDemo {
    public static void main(String[] args) throws ParseException {
        var t = new Timer();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date st = sdf.parse("2021-08-24 17:49:10");
        t.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println(sdf.format(new Date()));
            }
        }, st, 3000);

        var t2 = new Timer();
        t2.schedule(new TimerTask() {
            @Override
            public void run() {
                t.cancel();
                t2.cancel();
            }
        }, 1000 * 20);


        /*指定的日期时间对象，开始执行一次
        var c = Calendar.getInstance();
        c.add(Calendar.SECOND, 30);
        Date st = c.getTime();
        System.out.printf("%1$tF %1$tT %n", System.currentTimeMillis());
        t.schedule(new TimerTask() {
            @Override
            public void run() {
                //System.out.println("helloworld");
                System.out.printf("%1$tF %1$tT %n", st);
                t.cancel();
            }
        }, st);*/

        /*5秒开始执行，以后每一秒执行一次
        t.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("hello world");
            }
        }, 5000, 1000);*/

        /*3秒后执行一次，就完成停止
        t.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("hello world");
                t.cancel();
            }
        }, 3000);*/

    }
}
